/*
 * Copyright (c) 2013 WDCY Information Technology Co. Ltd
 * www.wdcy.cc
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * WDCY Information Technology Co. Ltd ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement you
 * entered into with WDCY Information Technology Co. Ltd.
 */
package com.monkey.web.platform.controller;

import com.monkey.core.domain.code.CodeTemplate;
import com.monkey.core.plugin.dto.MonkeyDto;
import com.monkey.core.repository.search.Searchable;
import com.monkey.core.web.response.MonkeyResponseDto;
import com.monkey.web.platform.infrastructure.AbstractUploadController;
import com.monkey.web.platform.service.CodeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author Wu Tianqiang
 */
@Controller
@RequestMapping(value = "/m", produces = {MediaType.APPLICATION_JSON_VALUE})
public class CodeTemplateController extends AbstractUploadController {
    private static final String CODE_RESOURCE_BASE_PATH = "/code";
    @Autowired
    private CodeTemplateService codeTemplateService;

    private List<String> allowFiles = Arrays.asList(".png", ".jpg", ".jpeg", ".bmp");

    /**
     * @param monkeyDto
     * @return
     */

//    @ParameterConstraints(value = {
//            @ParameterConstraint(
//                    name = "modelParam",
//                    constraintType = {ConstraintType.NotBlank},
//                    notBlank = @NotBlank(message = "model参数不能为空")
//            )
//    })
    @RequestMapping(value = "/code/generate", method = RequestMethod.POST)
    public String generate(MonkeyDto monkeyDto) {
        monkeyDto.println();
        codeTemplateService.startGenerate(monkeyDto);
        return "SUCCESS:代码生成完成";
    }

    @RequestMapping(value = "/templates", method = RequestMethod.GET)
    public Page<CodeTemplate> templates(Searchable searchable) {
        searchable.addSort(Sort.Direction.DESC, "createTime");
        return codeTemplateService.findAll(searchable);
    }

    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public String addTemplate(MonkeyDto monkeyDto) {
        monkeyDto.println();
        codeTemplateService.addTemplate(monkeyDto);
        return "SUCCESS:新增模板成功";
    }

    @RequestMapping(value = "/template", method = RequestMethod.PUT)
    public String modifyTemplate(MonkeyDto monkeyDto) {
        monkeyDto.println();
        codeTemplateService.modifyTemplate(monkeyDto);
        return "SUCCESS:修改模板成功";
    }

    @Override
    protected List<String> getAllowFileTypes() {
        return allowFiles;
    }

    @RequestMapping(value = "/template/pic/upload", method = RequestMethod.POST)
    public MonkeyResponseDto upload(@RequestParam MultipartFile file) {
        return uploadFile(file);
    }

    @RequestMapping(value = "/template/pic/select", method = RequestMethod.PUT)
    public String usePicture(@RequestParam Long templateId, @RequestParam String path) throws IOException {
        String relateTempPath = getResourceRelatePath(path);
        String realPath = fileService.store(relateTempPath, CODE_RESOURCE_BASE_PATH);
        CodeTemplate template = codeTemplateService.findOne(templateId);
        template.setPicture(realPath);
        codeTemplateService.save(template);
        return "SUCCESS:预览图片保存成功";
    }


    @RequestMapping(value = "/template/{ids}", method = RequestMethod.DELETE)
    public String removeTemplate(@PathVariable Long[] ids) {
        codeTemplateService.delete(ids);
        return "SUCCESS:删除成功";
    }


}